﻿using System.Linq;
using System.Text.RegularExpressions;
using System.Windows.Documents;
using System.Windows.Media;
using System.Xml.Linq;

namespace ComicTool.Drawing.Svg
{
    public class TextSpan
    {
        private readonly TextInfo textInfo;
        //private Size size;
        private string text;

        public TextSpan(TextInfo textInfo, XElement nav, bool first, bool last)
        {
            this.textInfo = textInfo;
            text = nav.Value;
            textInfo.LoadInfo(nav.Attributes().ToList());

            LoadText(first, last);
        }

        public TextSpan(TextInfo textInfo, string t, bool first, bool last)
        {
            this.textInfo = textInfo;
            text = t;
            LoadText(first, last);
        }

        //public Size Size
        //{
        //    get { return size; }
        //}

        private void LoadText(bool first, bool last)
        {
            if (textInfo.preserveWS)
            {
                text = text.Replace("\r\n", " ").Replace("\n", " ").Replace("\t", " ");
            }
            else
            {
                text = text.Replace("\r\n", "").Replace("\n", "").Replace("\t", " ");
                if (first)
                {
                    text = text.TrimStart();
                }
                if (last)
                {
                    text = text.TrimEnd();
                }
                var r = new Regex(@"\s{2,}");
                text = r.Replace(text, " ");
            }
        }

        public Run Draw()
        {
            var block = new Run();
            block.Text = text;
            //run.FontSize = size;
            if (!textInfo.fontFamily.IsNullOrEmpty())
            {
                block.FontFamily = new FontFamily(textInfo.fontFamily);
            }
            block.FontStyle = textInfo.fontStyle;
            block.FontWeight = textInfo.fontWeight;
            block.FlowDirection = textInfo.textDirection;
            //StringFormat format = (StringFormat)StringFormat.GenericTypographic.Clone();
            //format.LineAlignment = textInfo.lineAlignment;
            //format.FormatFlags |= StringFormatFlags.MeasureTrailingSpaces;
            //if (textInfo.rightToLeft)
            //    format.FormatFlags |= StringFormatFlags.DirectionRightToLeft;
            //if (textInfo.vertical)
            //    format.FormatFlags |= StringFormatFlags.DirectionVertical;

            //size = g.MeasureString(text, font, PointF.Empty, format);
            //if (textInfo.vertical)
            //    size.Width = 0;
            //else
            //    size.Height = 0;

            //if (textInfo.addDescent)
            //{
            //    float delta = font.Size * font.FontFamily.GetCellDescent(font.Style) / font.FontFamily.GetEmHeight(font.Style);
            //    if (textInfo.vertical)
            //        pos.X -= delta;
            //    else
            //        pos.Y += delta;
            //}
            block.Foreground = textInfo.fill;
            return block;

            //Matrix mOld = g.Transform;
            //Matrix mNew = g.Transform;
            //mNew.Multiply(textInfo.transform);
            //g.Transform = mNew;
            //if (textInfo.fill != null)
            //    g.FillPath(textInfo.fill, path);
            //if (textInfo.stroke != null)
            //    g.DrawPath(textInfo.stroke, path);
            //g.Transform = mOld;
        }
    }
}
